Published

December 3, 2024

Preamble

Dependencies

Show the code
library(ggplot2)
library(RColorBrewer)
library(ExperimentHub)
library(SpatialExperiment)
library(STexampleData)

Molecule-based dataset

Setup

Show the code
eh <- ExperimentHub()
q <- query(eh, "MERFISH")
df <- eh[["EH7546"]]

Wrangling

Show the code
# extract cell metadata
i <- seq_len(9)
cd <- data.frame(df[, i], row.names = 1)

# set sample identifiers
id <- grep("Bregma", names(cd))
names(cd)[id] <- "sample_id"

# rename spatial coordinates
xy <- grep("Centroid", names(cd))
xy <- names(cd)[xy] <- c("x", "y")

# simplify annotations
cd$cluster_id <- cd$Cell_class
for (. in c("Endothelial", "OD Mature", "OD Immature"))
  cd$cluster_id[grep(., cd$cluster_id)] <- .

# extract & sparsify assay data
y <- data.frame(df[, -i], row.names = df[, 1])
y <- as(t(as.matrix(y)), "dgCMatrix")

# construct SPE
(spe <- SpatialExperiment(
  assays = list(exprs  = y),
  spatialCoordsNames = xy,
  colData = cd))
class: SpatialExperiment 
dim: 161 73655 
metadata(0):
assays(1): exprs
rownames(161): Ace2 Adora2a ... Ucn3 Vgf
rowData names(0):
colnames(73655): 6749ccb4-2ed1-4029-968f-820a287f43c8
  6cac74bd-4ea7-4701-8701-42563cc65eb8 ...
  6b666f81-7b73-4100-9e02-b5381b39f0f3
  fdcddd97-7701-462a-b48f-979111245bd5
colData names(7): Animal_ID Animal_sex ... Neuron_cluster_ID cluster_id
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):
spatialCoords names(2) : x y
imgData names(0):
Show the code
gg <- data.frame(spatialCoords(spe), colData(spe))
pal <- brewer.pal(length(unique(gg$cluster_id)), "Paired")
ggplot(gg, aes(x, y, col = cluster_id)) +
  facet_wrap(~ sample_id, scales = "free") +
  geom_point(size = 0.1) + scale_color_manual(values = pal) +
  guides(col = guide_legend(override.aes = list(size = 2))) +
  theme_void() + theme(legend.key.size = unit(0.5, "lines"))

Save data

Show the code
# Define the directory and file paths
dir_path <- "../data"

# Check if the directory exists, and create it if it doesn't
if (!dir.exists(dir_path)) {
  dir.create(dir_path)
}

saveRDS(spe, "../data/spe.rds")

Appendix

Session info

Show the code
sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.7.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] MerfishData_1.2.0           EBImage_4.42.0             
 [3] STexampleData_1.8.0         SpatialExperiment_1.10.0   
 [5] SingleCellExperiment_1.22.0 SummarizedExperiment_1.30.2
 [7] Biobase_2.60.0              GenomicRanges_1.52.1       
 [9] GenomeInfoDb_1.36.4         IRanges_2.34.1             
[11] S4Vectors_0.38.2            MatrixGenerics_1.12.3      
[13] matrixStats_1.0.0           ExperimentHub_2.8.1        
[15] AnnotationHub_3.8.0         BiocFileCache_2.8.0        
[17] dbplyr_2.3.4                BiocGenerics_0.46.0        
[19] RColorBrewer_1.1-3          ggplot2_3.4.4              

loaded via a namespace (and not attached):
 [1] DBI_1.1.3                     bitops_1.0-7                 
 [3] rlang_1.1.1                   magrittr_2.0.3               
 [5] compiler_4.3.1                RSQLite_2.3.1                
 [7] DelayedMatrixStats_1.22.6     fftwtools_0.9-11             
 [9] png_0.1-8                     vctrs_0.6.4                  
[11] pkgconfig_2.0.3               crayon_1.5.2                 
[13] fastmap_1.1.1                 magick_2.8.0                 
[15] XVector_0.40.0                ellipsis_0.3.2               
[17] labeling_0.4.3                scuttle_1.10.3               
[19] utf8_1.2.3                    promises_1.2.1               
[21] rmarkdown_2.25                purrr_1.0.2                  
[23] bit_4.0.5                     xfun_0.40                    
[25] beachmat_2.16.0               zlibbioc_1.46.0              
[27] cachem_1.0.8                  jsonlite_1.8.7               
[29] blob_1.2.4                    later_1.3.1                  
[31] rhdf5filters_1.12.1           DelayedArray_0.26.7          
[33] Rhdf5lib_1.22.1               BiocParallel_1.34.2          
[35] interactiveDisplayBase_1.38.0 jpeg_0.1-10                  
[37] tiff_0.1-12                   parallel_4.3.1               
[39] R6_2.5.1                      limma_3.56.2                 
[41] Rcpp_1.0.11                   knitr_1.44                   
[43] R.utils_2.12.2                httpuv_1.6.11                
[45] Matrix_1.5-4.1                tidyselect_1.2.0             
[47] rstudioapi_0.15.0             abind_1.4-5                  
[49] yaml_2.3.7                    codetools_0.2-19             
[51] curl_5.1.0                    lattice_0.21-8               
[53] tibble_3.2.1                  shiny_1.7.5.1                
[55] withr_2.5.1                   KEGGREST_1.40.1              
[57] evaluate_0.22                 Biostrings_2.68.1            
[59] pillar_1.9.0                  BiocManager_1.30.22          
[61] filelock_1.0.2                generics_0.1.3               
[63] RCurl_1.98-1.12               BiocVersion_3.17.1           
[65] sparseMatrixStats_1.12.2      munsell_0.5.0                
[67] scales_1.3.0                  xtable_1.8-4                 
[69] glue_1.6.2                    tools_4.3.1                  
[71] locfit_1.5-9.8                rhdf5_2.44.0                 
[73] grid_4.3.1                    edgeR_3.42.4                 
[75] DropletUtils_1.20.0           AnnotationDbi_1.62.2         
[77] colorspace_2.1-0              GenomeInfoDbData_1.2.10      
[79] HDF5Array_1.28.1              cli_3.6.1                    
[81] rappdirs_0.3.3                fansi_1.0.5                  
[83] S4Arrays_1.0.6                dplyr_1.1.3                  
[85] gtable_0.3.4                  R.methodsS3_1.8.2            
[87] digest_0.6.33                 dqrng_0.3.1                  
[89] farver_2.1.1                  rjson_0.2.21                 
[91] htmlwidgets_1.6.2             memoise_2.0.1                
[93] htmltools_0.5.6.1             R.oo_1.25.0                  
[95] lifecycle_1.0.3               httr_1.4.7                   
[97] mime_0.12                     bit64_4.0.5                  

©2024 The pasta authors. Content is published under Creative Commons CC-BY-4.0 License for the text and GPL-3 License for any code.